const TOKEN_KEY = "APP-TOKEN-2020"

export default {
  toFd(obj) {
    let fd = new FormData()
    for (let k in obj) {
      fd.append(k, obj[k])
    }
    return fd
  },
  setToken(token) {
    sessionStorage.setItem(TOKEN_KEY, token)
  },
  getToken() {
    return sessionStorage.getItem(TOKEN_KEY) || ""
  },
  clearToken() {
    sessionStorage.removeItem(TOKEN_KEY)
  },
  /**
   * 自动化检测多选题的选项
   * @param nv   检查的列表
   * @param key  检测的关键值
   * @param vSet 对象集合
   * @param opts 被渲染的集合
   */
  checkOpts(nv, key, vSet, opts) {
    if (nv.indexOf(key) != -1) {
      if (nv.length > 1) {
        vSet.splice(0, vSet.length, key)
      }
      opts.filter(v => v.value != key).forEach(v => {
        v.disable = true
      })
    } else {
      opts.filter(v => v.value != key).forEach(v => {
        v.disable = false
      })
    }
  },
  isBlank(val) {
    return val == null || val.trim().length == 0
  },
}